package com.cnap.db.handler;

import cn.hutool.core.text.CharSequenceUtil;
import com.cnap.model.application.DeployResourceInfo;
import com.cnap.utils.JsonUtil;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 应用标签handler
 *
 * @author xc
 **/
public class DeployResourceInfoHandler implements TypeHandler<DeployResourceInfo> {
    /**
     * setParameter
     *
     * @param preparedStatement preparedStatement
     * @param i                 i
     * @param applicationLabel  applicationLabel
     * @param jdbcType          jdbcType
     * @throws SQLException SQLException
     */
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, DeployResourceInfo applicationLabel, JdbcType jdbcType) throws SQLException {
        String str = JsonUtil.objectToJsonStr(applicationLabel);
        preparedStatement.setString(i, str);
    }

    /**
     * getResult
     *
     * @param resultSet resultSet
     * @param s         s
     * @return ApplicationLabel
     * @throws SQLException SQLException
     */
    @Override
    public DeployResourceInfo getResult(ResultSet resultSet, String s) throws SQLException {
        String str = resultSet.getString(s);
        if (CharSequenceUtil.isNotBlank(str)) {
            return JsonUtil.jsonStrToObject(str, DeployResourceInfo.class);
        }
        return null;
    }

    /**
     * getResult
     *
     * @param resultSet resultSet
     * @param i         i
     * @return ApplicationLabel
     * @throws SQLException SQLException
     */
    @Override
    public DeployResourceInfo getResult(ResultSet resultSet, int i) throws SQLException {
        String str = resultSet.getString(i);
        if (CharSequenceUtil.isNotBlank(str)) {
            return JsonUtil.jsonStrToObject(str, DeployResourceInfo.class);
        }
        return null;
    }

    /**
     * getResult
     *
     * @param callableStatement callableStatement
     * @param i                 i
     * @return ApplicationLabel
     * @throws SQLException SQLException
     */
    @Override
    public DeployResourceInfo getResult(CallableStatement callableStatement, int i) throws SQLException {
        String str = callableStatement.getString(i);
        if (CharSequenceUtil.isNotBlank(str)) {
            return JsonUtil.jsonStrToObject(str, DeployResourceInfo.class);
        }
        return null;
    }
}
